﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BestBuy.Common;
namespace BestBuy.DAL
{
    public class cManageSellOffer
    {
        public static int getIdCompany(int idUser)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.ThongTinTaiKhoans.Where(p => p.IDTaiKhoan == idUser).FirstOrDefault().IDCongTy.Value;
        }
        public static int getCountSellOffer(int idUser)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.BangTinBans.Where(p => p.IDTaiKhoan == idUser).ToList().Count;
        }
        public static string getNameCompany(int idCompany)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.CongTies.Where(p => p.IDCongTy == idCompany).FirstOrDefault().TenCongTy;
        }
        public static List<BangTinBan> getListSellOffer(int idUser)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.BangTinBans.Where(p => p.IDTaiKhoan == idUser && (p.TinhTrang == (int)BangTinBanStatus.Unverified || p.TinhTrang == (int)BangTinBanStatus.Active)).ToList();
        }
        public static void deleteSellOffer(int id)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            var kq = context.BangTinBans.Where(p => p.IDBangTinBan == id).FirstOrDefault();
            if (kq != null)
            {
                kq.TinhTrang = (int)BangTinBanStatus.Deleted;
                context.SubmitChanges();
            }
        }
        public static void rePost(int id)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            var kq = context.BangTinBans.Where(p => p.IDBangTinBan == id).FirstOrDefault();
            if (kq != null)
            {
                kq.NgayDang = DateTime.Now.ToShortDateString();
                context.SubmitChanges();
            }
        }

        public static BangTinBan loadInfo(int id)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.BangTinBans.Where(p => p.IDBangTinBan == id).FirstOrDefault();
        }
        public static List<DanhMucBan> loadcategory(int id)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.DanhMucBans.Where(p => p.IDBangTinBan == id).ToList();
        }
        public static String getNamCategory(int id)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.DanhMucs.Where(p => p.IDDanhMuc == id).First().TenDanhMuc;
        }
        public static void UpdateSellOffer(BangTinBan tb)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            var kq = context.BangTinBans.Where(p => p.IDBangTinBan == tb.IDBangTinBan).First();
            kq.TieuDe = tb.TieuDe;
            kq.TuKhoa = tb.TuKhoa;
            kq.MoTa = tb.MoTa;
            kq.ThoiHanDang = tb.ThoiHanDang;
            kq.HinhAnh = tb.HinhAnh;
            kq.KyHanGiaoHang = tb.KyHanGiaoHang;
            kq.KhaNangCungCap = tb.KhaNangCungCap;
            kq.NoiXuatXu = tb.NoiXuatXu;
            kq.SoLuongDatToiThieu = tb.SoLuongDatToiThieu;
            kq.PhuongThucThanhToan = tb.PhuongThucThanhToan;
            context.SubmitChanges();
        }
        public static void clearCategory(int idtin)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            var kq = context.DanhMucBans.Where(p => p.IDBangTinBan == idtin).ToList();
            context.DanhMucBans.DeleteAllOnSubmit(kq);
            context.SubmitChanges();
        }
        public static void UpdateCategorySellOffer( int idtin, int iddm)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            DanhMucBan dmb = new DanhMucBan();
            dmb.IDBangTinBan = idtin;
            dmb.IDDanhMuc = iddm;
            context.DanhMucBans.InsertOnSubmit(dmb);
            context.SubmitChanges();
        }
        public static List<BangTinBan> searchTitle(int iduser, String name)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            var kq = context.BangTinBans.Where(p => p.IDTaiKhoan == iduser && p.TieuDe.CompareTo(name) == 0).ToList();
            return kq;
        }
    }
}